package durst.action.account;

import java.sql.SQLException;
import java.util.List;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;

import com.sdicons.json.mapper.JSONMapper;
import com.sdicons.json.mapper.MapperException;

import durst.action.CalendarBasicAction;
import durst.data.CSDMessage;
import durst.data.dao.DaoFactory;
import durst.data.dao.impl.MessageDao;

public class InboxViewAction extends CalendarBasicAction {
	@SuppressWarnings("unchecked")
	@DefaultHandler
	public Resolution view() throws SQLException, MapperException {
		MessageDao<CSDMessage, Integer> dao = (MessageDao<CSDMessage, Integer>) DaoFactory
				.getInstance().createDao(CSDMessage.class);
		List<CSDMessage> messages = dao.getRecievedMessages(context.getAccount());
		List<CSDMessage> sent = dao.getSentMessages(context.getAccount());
		
		context.getRequest().setAttribute("messageCount", messages.size());
		context.getRequest().setAttribute("messages", JSONMapper.toJSON(messages).render(false));
		
		context.getRequest().setAttribute("sentCount", sent.size());
		context.getRequest().setAttribute("sentMessages", JSONMapper.toJSON(sent).render(false));
		
		return new ForwardResolution("/account/inbox.jsp");
	}
}
